﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DynamicPageRoute.cs" company="ecblog.codeplex.com">
//   emrecoskun.net - ibrahimatay.com
// </copyright>
// <summary>
//   The dynamic page route
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace EcBlog.Routing
{
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;

    using EcBlog.Composition;
    using EcBlog.Data;

    /// <summary>
    /// The dynamic page route
    /// </summary>
    public class DynamicPageRoute : RouteBase
    {
        /// <summary>
        /// When overridden in a derived class, returns route information about the request.
        /// </summary>
        /// <param name="httpContext">An object that encapsulates information about the HTTP request.</param>
        /// <returns>
        /// An object that contains the values from the route definition if the route matches the current request, or null if the route does not match the request.
        /// </returns>
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            var unitOfWork =
                CompositionDependencyResolver.StaticFactory.GetService(typeof(IEcUnitOfWork)) as IEcUnitOfWork;
            if (unitOfWork == null)
            {
                return null;
            }

            var routeHandler = new MvcRouteHandler();
            var currentRoute = new Route("{*URI}", routeHandler);
            var routeData = new RouteData(currentRoute, routeHandler);
            var slug = httpContext.Request.RawUrl;
            if (slug.Length > 255)
            {
                slug = slug.Substring(0, 254);
            }

            var page = unitOfWork.PageRepository.Get().FirstOrDefault(c => c.Slug == slug);
            if (page != null)
            {
                routeData.Values["controller"] = "Blog";
                routeData.Values["action"] = "DynamicPageSlug";
                routeData.Values["pageSlug"] = slug;
                return routeData;
            }

            return null;
        }

        /// <summary>
        /// When overridden in a derived class, checks whether the route matches the specified values, and if so, generates a URL and retrieves information about the route.
        /// </summary>
        /// <param name="requestContext">An object that encapsulates information about the requested route.</param>
        /// <param name="values">An object that contains the parameters for a route.</param>
        /// <returns>
        /// An object that contains the generated URL and information about the route, or null if the route does not match <paramref name="values"/>.
        /// </returns>
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            return null; 
        }
    }
}